package com.yxx.service.LogService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxx.entity.OperationLog;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.query.LogQuery;
import com.yxx.entityModel.vo.response.OperationLogResponse;
import com.yxx.mapper.OperationLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 操作日志服务
 * @author yuxiangxun
 * @date 2025/2/15
 * @apiNote
 */
@Service("operationLogService")
public class OperationLogService extends ServiceImpl<OperationLogMapper, OperationLog> {

    @Resource
    private OperationLogMapper operationLogMapper;

    public void saveOperationLog(OperationLog operationLog) {
        // 保存操作日志
        operationLogMapper.insert(operationLog);
    }

    /**
     * 分页查询操作日志
     * @param logQuery
     * @return
     */
    public PageResult<OperationLogResponse> listOperationLogVO(LogQuery logQuery) {
        // 查询操作日志数量
        Long count = operationLogMapper.selectCount(new LambdaQueryWrapper<OperationLog>()
                .like(StringUtils.hasText(logQuery.getOptModule()), OperationLog::getModule, logQuery.getOptModule())
                .or()
                .like(StringUtils.hasText(logQuery.getKeyword()), OperationLog::getDescription, logQuery.getKeyword())
        );
        if (count == 0) {
            return new PageResult<>();
        }
        // 查询操作日志列表
        List<OperationLogResponse> operationLogRespList = operationLogMapper.selectOperationLogVOList(logQuery);
        return new PageResult<>(operationLogRespList, count);
    }
}
